import React from "react";
import BackModal from "@/component/backModal";
import { dropByCacheKey, getCachingKeys } from "react-router-cache-route";
import { Toast } from "antd-mobile";

let current = 0;

/**
 * 重要提示：
 * 此处流程配置，三只提交便授信提交，
 * 如若配置三要素不提交授信，
 * 请在三要素组件中做适配
 * steps: 流程页面
 * backIcon： 挽留页图
 */

const creditMap = {
	603: {
		steps: ["twoElement", "liveness"],
		backIcon: 'xhfBackIcon30'
	},
	610: {
		steps: ["fourElement", "liveness"],
		backIcon: 'xhfBackIcon30'
	},
	614: {
		steps: ["threeElement"],
		backIcon: 'xhfBackIcon50'
	},
	617: {
		steps: ["twoElement", "zxAgrement", "liveness"],
		backIcon: 'xhfBackIcon30'
	}
};

let currentStep = [];
let currentCode = ""; //当前授信标识

const creditStore = WrapperComponent => {
	return class extends React.Component {
		constructor(props) {
			super(props);
			this.state = {
				isVisible: false,
				isHiddenBotton: false,
				backIconByLnid: ""
			};
		}
		componentDidMount() {
			this.props.history.listen(route => {
				let path = route.pathname.split("/").pop();
				const search = route.search;
				current = currentStep.indexOf(path + search) + 1;
			});
		}
		showModal = () => {
			this.setState({
				isVisible: true,
				backIconByLnid: creditMap[currentCode].backIcon
			});
		};
		handleBack = () => {
			this.showModal();
		};
		onModalClick = booleanValue => {
			this.setState({
				isVisible: false
			});
			if (!booleanValue) {
				this.dropKey();
				this.props.history.push("/xhfHomeNoCredit");
			}
		};
		dropKey = () => {
			getCachingKeys().forEach(value => {
				dropByCacheKey(value);
			});
		};
		//去空格
		trim = (values = {}) => {
			Object.keys(values).forEach(key => {
				if (typeof values[key] === "string") {
					values[key] = values[key].replace(/\s*/g, "");
				}
			});
		};
		getCreditStep = code => {
			const step = creditMap[code];
			currentCode = code;
			if (step.toast) {
				return Toast.info(step.toast);
			}
			currentStep = step.steps;
			this.goNext();
		};
		goNext = (openStatus, failMessage) => {
			console.log(current, currentStep[current]);
			if (current !== -1) {
				let path = currentStep[current];
				if (path) {
					//后续还有流程
					this.props.history.push(`/xhf/credit/${path}`);
				} else {
					// 后续没有流程，并且有传入开通状态
					if (openStatus) {
						if (openStatus == "1") {
							//授信成功
							this.props.history.push({
								pathname: "/xhfHomeCredit"
							});
						} else if (openStatus == "4") {
							//授信失败
							this.props.history.push({
								pathname: "/xhfOpenFail",
								state: { message: failMessage }
							});
						} else if (openStatus == "2" || openStatus == "3") {
							//授信中,科罗纳去10秒倒计时页面
							this.props.history.push("/xhfLivenessRet");
						}
					} else {
						this.props.history.push("/xhfLivenessRet");
					}
				}
			}
		};
		render() {
			const { isVisible, isHiddenBotton, backIconByLnid } = this.state;
			console.log('高阶组件state更新：', this.state);
			return (
				<>
					<WrapperComponent
						{...this.props}
						goNext={this.goNext}
						getCreditStep={this.getCreditStep}
						handleBack={this.handleBack}
						productNum={601}
						currentCode={currentCode}
						current={current}
						trim={this.trim}
						isHiddenBotton={isHiddenBotton}
					/>
					<BackModal
						product={backIconByLnid}
						isVisible={isVisible}
						onClick={this.onModalClick}
					/>
				</>
			);
		}
	};
};

export default creditStore;
